<?php

namespace yak\layui\controllers;

use kordar\wxyii2\actions\MPMediaUploadAction;
use yak\layui\actions\YakErrorAction;
use yak\layui\models\admin\LoginForm;
use yak\layui\models\admin\User;
use yak\layui\models\AdminConfig;
use Yii;

class DefaultController extends BaseController
{
    protected $authExcept = [
        'login', 'captcha', 'upload'
    ];

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => YakErrorAction::class,
                'renderHtmlLayout' => function () {
                    $request = Yii::$app->request;
                    return $request->get('iframe') ? 'blank' : 'main';
                }
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
            'upload' => [
                'class' => MPMediaUploadAction::class,
                'type' => 'image',
                'mediaId' => '',
                'accessToken' => '32_9sgpwoMBYKOezP78kyocJijbJm3fm24U_Lyv1iKFbjhfYgxaJ9TfSQM6bRkJqgPY01079zR3SRgq1TwjHP9qfijgR_y7C2HwhhN1hGMRx92B3kDKEBp6MUTIgjG5WwQa4JuE8GST63SSGu59XSBfACAFAJ'
            ]
        ];
    }

    // public function actionError() {}
    // public function actionCaptcha() {}

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index', ['model' => new User()]);
    }

    /**
     * @return string
     */
    public function actionLogin()
    {
        Yii::$app->user->logout();
        $model = new LoginForm();

        if (Yii::$app->request->isAjax) {
            if (!Yii::$app->user->isGuest) {
                return $this->ajaxRedirect(Yii::$app->getHomeUrl());
            }
            if ($model->load(Yii::$app->request->post(), 'Data') && $model->login())
                return $this->ajaxRedirect(Yii::$app->getUser()->getReturnUrl());
            else
                return $this->ajaxError(current($model->getFirstErrors()));
        }

        if (!Yii::$app->user->isGuest) {
            return $this->redirect(Yii::$app->getHomeUrl());
        }

        $this->layout = 'login';
        return $this->render('login', [ 'model' => $model ]);
    }

    /**
     * @return \yii\web\Response
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();
        return $this->goHome();
    }

    public function actionTheme()
    {
        $id = Yii::$app->user->getId();
        $model = AdminConfig::findOne($id) ?: new AdminConfig(['id' => $id]);
        if (Yii::$app->request->isAjax) {
            if ($model->load(Yii::$app->request->post(), 'Data') && $model->save()) {
                return $this->ajaxSuccess();
            } else {
                return $this->ajaxError(current($model->getFirstErrors()));
            }
        }

        $this->layout = 'blank';
        return $this->render('theme', [
            'model' => $model,
            'ajaxOptions' => [
                'modelClass' => $this->ajaxHeaderClass($model),
                'modelMethod' => 'themeSave'
            ]
        ]);
    }

}
